// pages/details/details.js
var WxParse = require('../../wxParse/wxParse.js');
var {getdetailData} = require('../../api/goods.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //   goodsList:{
        //     id:5,
        //     goodsName:"商品名称",
        //     goodsImage:"../../image/goods01.jpg",
        //     goodsImgs:[
        //         "../../image/goods01.jpg",
        //         "../../image/goods02.jpg",
        //         "../../image/goods03.jpg"
        //     ],
        //     goodsPrice:"210",
        //     goodsPriceOld:"300",
        //     goodsDetails:"../../image/IMG_0466.JPG"
        //   },
        goodsList:{},
        num:0,
        error:'',
        flag:false
    },
    goCart(){
        wx.switchTab({
          url: '/pages/car/car',
        })
    },
    goHome(){
        wx.switchTab({
          url: '/pages/home/home',
        })
    },
    goPay(){
        if(wx.getStorageSync('GoodsCarLists').length == 0){
            this.setData({
                error: '请加入购物车'
            });
            return;
        }
        if( wx.getStorageSync('GoodsCarLists')[0].goodsPrice == 0 ){
            this.setData({
                error: '该商品暂时无法购买'
            });
            return;
        }
        let date=new Date();
        //年
        let year=date.getFullYear();
        //月
        let month=date.getMonth()+1;
        //日
        let day=date.getDate();
        //时
        let hh=date.getHours();
        //分
        let m=date.getMinutes();
        let mm = m < 10? "0" + m : m
        //秒
        let s=date.getSeconds();
        let  ss =s<10?"0"+s:s;
        let rq=year+"-"+month+"-"+day+"-"+hh+":"+mm+":"+ss;

        let whole = [];
        let wholes = wx.getStorageSync('wholes');
        let GoodsCarLists = wx.getStorageSync('GoodsCarLists');//购物车数据
        GoodsCarLists.forEach(element => {
            let {id,goodsName,goodsPrice,goodsImage,num} = element
            let goodswpay={
                flag:1,
                id,
                goodsName,
                goodsPrice,
                goodsImage,
                num,
                rq,
                jude:true,
                
            }
            whole.push(goodswpay);
        });
        if(wholes){
            for(let i = 0;i <whole.length;i++){
                wholes.push(whole[i]);
                wx.setStorageSync('wholes',wholes)
            }
        }else{
            wx.setStorageSync('wholes',whole);
        }
        
        wx.navigateTo({
          url: '/pages/pay/pay',
        })
    },
    tapDialogButton(){
        this.setData({
            error: ''
        });
        return;
    },
    getCart(){
        // console.log(this.data.goodsList);
        var CartLists = {
            id:this.data.goodsList.id,
            goodsName:this.data.goodsList.goodsName,
            goodsPrice:this.data.goodsList.goodsPrice,
            goodsImage:this.data.goodsList.goodsImage,
            goodsAddress:this.data.goodsList.address,
            num:1,
        }
        let GoodsCarLists = wx.getStorageSync('GoodsCarLists');
        if(GoodsCarLists){
            let index = GoodsCarLists.findIndex(item=>{
                return item.id == this.data.goodsList.id;
            })
            if(index != -1){
                GoodsCarLists[index].num += 1;
            }else{
                GoodsCarLists.push(CartLists);
            }
            wx.setStorageSync('GoodsCarLists', GoodsCarLists)
        }else{
            wx.setStorageSync('GoodsCarLists', [CartLists])
        }
        this.setData({
            num:wx.getStorageSync('GoodsCarLists').length
        })
    },
    getGoodsData(id){
        getdetailData({goods_id:id}).then(data=>{
            console.log(data.message);
            if(data.message){
                let{goods_id, goods_name, goods_small_logo,goods_price,pics,goods_introduce} = data.message;
                let datas = {
                    id: goods_id,
                    goodsName: goods_name,
                    goodsImage: goods_small_logo,
                    goodsImgs: pics,
                    goodsPrice: goods_price,
                    goodsPriceOld: goods_price + 400,
                    goodsDetails: goods_introduce                    
                }
                // html转富文本方式
                WxParse.wxParse('goods_introduce', 'html', goods_introduce, this, 5);
                this.setData({
                    goodsList:datas
                })
            }
        }).then(()=>{
            this.coll();
        })

    },
    del(){
        this.setData({
            flag:false
        })
        let shoucan = wx.getStorageSync('shoucan');
        if(shoucan){
            let index = shoucan.findIndex(item=>{
                return item.id == this.data.goodsList.id;
            })
            shoucan.splice(index,1);
            wx.setStorageSync('shoucan', shoucan)
        }else{
            wx.setStorageSync('shoucan', shoucang)
        }
    },
    add(){
        this.setData({
            flag:true
        })
        console.log("123",this.data.goodsList);
        let shoucan = wx.getStorageSync('shoucan');
        let shoucang = [];
        shoucang.push(this.data.goodsList);
        if(shoucan){
            let index = shoucan.findIndex(item=>{
                return item.id == this.data.goodsList.id;
            })
            if(index == -1){
                shoucan.push(this.data.goodsList);
            }
            wx.setStorageSync('shoucan', shoucan)
        }else{
            wx.setStorageSync('shoucan', shoucang)
        }
    },
    coll(){
        let shoucan = wx.getStorageSync('shoucan'); 
        if(shoucan){
            let index = shoucan.findIndex(item=>{
                return item.id == this.data.goodsList.id;
            })
            if(index != -1 ){
                this.setData({
                    flag:true
                })
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getGoodsData(options.id);
        // getdetailData({goods_id:57444}).then(data=>{
        //     console.log(data);
        // })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        console.log(this.data.goodsList);
        this.setData({
            num:wx.getStorageSync('GoodsCarLists').length||0
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        var goodsLists = wx.getStorageSync('GoodsCarList');
        this.setData({
            goodsList:goodsLists,
        });
        this.getGoodsData();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})